package com.soft.bams.cilent;


import com.soft.bams.entity.Account;
import com.soft.bams.exception.LoginException;
import com.soft.bams.exception.NotFoundException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;


public class LoginPanel extends JPanel {

    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    private void okButtonActionPerformed(ActionEvent e) {

        String idStr = textField.getText();
        String password = new String(passwordField.getPassword());


        if(idStr!=null && !idStr.equals("")){
            Long id = Long.valueOf(idStr);
            try {
                Account acc = atm.bank.login(id, password);

                if(acc!=null){

                    atm.acc = acc;
                    atm.businessPanel.initBusiness();


                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(),"businessPanel");


                    clearLogin();
                }
            } catch (LoginException | NotFoundException ex) {

                JOptionPane.showMessageDialog(null,ex.getMessage());
                passwordField.setText("");
            }
        }else{
            JOptionPane.showMessageDialog(null,"账户不能为空！");
        }
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"mainPanel");


        clearLogin();
    }

    public void clearLogin(){
        textField.setText("");
        passwordField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        title = new JLabel();
        idLabel = new JLabel();
        pwdLabel = new JLabel();
        textField = new JTextField();
        passwordField = new JPasswordField();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- title ----
        title.setText("\u767b  \u5f55  \u754c  \u9762");
        title.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title);
        title.setBounds(115, 70, 255, 40);

        //---- idLabel ----
        idLabel.setText("\u7528\u6237\u8d26\u53f7");
        idLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(idLabel);
        idLabel.setBounds(55, 140, 125, 32);

        //---- pwdLabel ----
        pwdLabel.setText("\u7528\u6237\u5bc6\u7801");
        pwdLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(55, 195, 125, 32);
        add(textField);
        textField.setBounds(190, 145, 200, 30);
        add(passwordField);
        passwordField.setBounds(190, 200, 200, 30);

        //---- okButton ----
        okButton.setText("\u767b  \u5f55");
        okButton.addActionListener(e -> okButtonActionPerformed(e));
        add(okButton);
        okButton.setBounds(115, 285, 92, 30);

        //---- cancelButton ----
        cancelButton.setText("\u53d6 \u6d88");
        cancelButton.addActionListener(e -> cancelButtonActionPerformed(e));
        add(cancelButton);
        cancelButton.setBounds(270, 285, 92, 30);

        setPreferredSize(new Dimension(480, 360));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel title;
    private JLabel idLabel;
    private JLabel pwdLabel;
    private JTextField textField;
    private JPasswordField passwordField;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
